package com.syezon.wifikey.download;


import com.hodanet.appadvplatclient.ReportAgent;
import com.hodanet.appadvplatclient.ad.AdInfo;
import com.syezon.android.base.download.DownloadTask;
import com.syezon.android.base.download.IDownloadTaskListener;
import com.syezon.android.base.utils.ApkUtil;
import com.syezon.android.base.utils.AppUtils;
import com.syezon.android.base.utils.LogUtils;
import com.syezon.android.base.utils.ToastUtils;
import com.syezon.wifikey.ad.AdPositionEnum;
import com.syezon.wifikey.ad.handler.AppInstallHelper;
import com.syezon.wifikey.channel.ChannelConfig;
import com.syezon.wifikey.notification.DownloadNotification;
import com.syezon.wifikey.utils.Utils;

/**
 * Created by Administrator on 2018/6/26.
 */

public class HodaAdDownloadListener implements IDownloadTaskListener {


    private static final String TAG = HodaAdDownloadListener.class.getSimpleName();

    private AdInfo adInfo;
    private AdPositionEnum adPositionEnum;

    public HodaAdDownloadListener(AdInfo adInfo, AdPositionEnum adPositionEnum) {
        this.adInfo = adInfo;
        this.adPositionEnum = adPositionEnum;
    }

    @Override
    public void onPrepare(DownloadTask downloadTask) {
        LogUtils.eTag(TAG, "onPrepare");
    }

    @Override
    public void onStart(DownloadTask downloadTask) {
        LogUtils.eTag(TAG, "onStart");
    }

    @Override
    public void onDownloading(DownloadTask downloadTask) {
        LogUtils.eTag(TAG, "onDownloading");
        try {
            DownloadNotification.getDownloadNotification(Utils.getApp()).notifyDownloadProgress(downloadTask, adInfo.getName());
        } catch (Exception e) {

        }
    }

    @Override
    public void onPause(DownloadTask downloadTask) {
        LogUtils.eTag(TAG, "onPause");
    }

    @Override
    public void onCancel(DownloadTask downloadTask) {
        LogUtils.eTag(TAG, "onCancel");
    }

    @Override
    public void onCompleted(DownloadTask downloadTask) {
        LogUtils.eTag(TAG, "onCompleted");
        try {
            String path = downloadTask.getSaveDirPath() + downloadTask.getFileName();
            ApkUtil.ApkInfo apkInfo = ApkUtil.getApkInfo(Utils.getApp(), path);
            if (apkInfo != null) {
                ApkUtil.installApk(Utils.getApp(), path);
            } else {
                ToastUtils.showShort("下载完成!");
            }
            DownloadNotification.getDownloadNotification(Utils.getApp()).notifyDownloadFinish(downloadTask, adInfo.getName());
            appDownloadComplete(adInfo, downloadTask.getSaveDirPath() + downloadTask.getFileName());
        } catch (Exception e) {
        }
    }

    /**
     * @param adInfo
     * @param path   下载文件路径
     */
    private void appDownloadComplete(AdInfo adInfo, String path) {
        try {
            ApkUtil.ApkInfo apkInfo = ApkUtil.getApkInfo(Utils.getApp(), path);
            if (apkInfo != null) {
                ReportAgent.reportAdvDownload(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
                ApkUtil.installApk(Utils.getApp(), path);
                AppInstallHelper.notifySelfAppInstallStart(apkInfo.pkgName, adInfo, adPositionEnum);
            }
        } catch (Exception e) {
        }
    }


    @Override
    public void onError(DownloadTask downloadTask, int errorCode) {
        LogUtils.eTag(TAG, "onError " + errorCode);
    }
}
